package com.etc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.stream.events.Comment;

import com.etc.entity.AnswerBean;
import com.etc.entity.CommentBean;
import com.etc.entity.User;
import com.etc.service.AnswerBeanService;
import com.etc.service.CommentBeanService;
import com.etc.service.impl.AnswerBeanServiceImpl;
import com.etc.service.impl.CommentBeanServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;


/**
 * 问答管理系统前台回答信息的展示
 * 回答列表展示+增加回答
 * 评论功能展示+增加评论
 * 点赞功能
 */
@WebServlet("/sas.do")
public class SingleQuestionAnswerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private CommentBeanService cbs=new CommentBeanServiceImpl();
	private AnswerBeanService as = new AnswerBeanServiceImpl();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SingleQuestionAnswerServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置响应和请求编码，获取op值
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//获取session
		HttpSession session=request.getSession();
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		
		int page =1;//默认第一页
		int pageSize = 10;//默认每页显示10条记录
		//从页面中获取page，pageSize
		if(request.getParameter("page")!=null)
		{
			page = Integer.parseInt(request.getParameter("page"));
		}
		
		if(request.getParameter("pageSize")!=null)
		{
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		
		//增加了模糊查询的部分;
		String keywords="";
		if(request.getParameter("keywords")!=null)
		{
			keywords = request.getParameter("keywords");
		}
		//增加了从页面中获取qid和aid ，通过这两个参数查询或者进行增删改查
		int qId=0;
		if(request.getParameter("qId")!=null)
		{
			qId = Integer.parseInt(request.getParameter("qId"));
			request.setAttribute("answerQid", qId);
		}
		int aid=0;
		if(request.getParameter("aid")!=null)
		{
			aid = Integer.parseInt(request.getParameter("aid"));
		}
		System.out.println(aid);
		//展示回答信息功能
		if(op.equals(""))
		{
			//通过service进行查询操作
			PageData<CommentBean> pcd=cbs.queryCommentByPageTimeSort(aid, page, pageSize, keywords);
			//将返回值PageData存起
			request.setAttribute("pdLoad", pcd);
			//通过service查询所有（包含question和user两个表）
			PageData<AnswerBean> pdn=as.queryAnswersByPageNo(qId,page, pageSize,keywords);
			//通过service查询所有（包含question和user和answer）
			PageData<AnswerBean> pd=as.queryAnswersByPage(qId,page, pageSize,keywords);
			pd.getData().forEach(System.out::println);
			//将返回的值进行存起
			request.setAttribute("pdQid", pd);
			request.setAttribute("pdNo", pdn);
			//要将每次模糊查询的关键字传回来给jsp
			request.setAttribute("keywords", keywords);
			// 从当前控制器跳转到jsp页面[问题列表]，跳转的方法叫做转发
			request.getRequestDispatcher("single.jsp").forward(request, response);
		//实现展示评论功能，并按时间排序+流加载
		}else if("seeComment".equals(op)) {
			//通过service进行查询操作
			PageData<CommentBean> pcd=cbs.queryCommentByPageTimeSort(aid, page, pageSize, keywords);
			//将返回值PageData存起
			request.setAttribute("pdLoad", pcd);
			//创建一个Gson类
			Gson gs = new Gson();
			//通过Gson的方法toJson将其转化为字符串
			String str = gs.toJson(pcd.getData());
			System.out.println("总页数"+str+pcd.getTotalPage());
			//并将字符串和总页数拼接，传入到页面中
			response.getWriter().print(str+pcd.getTotalPage());
		//实现回答问题的功能
		}else if("insertAnswer".equals(op)){
			//从页面中获取回答问题的信息
			String content=request.getParameter("content");
			//int userId=Integer.parseInt(request.getParameter("userId"));
			//通过登录的session取值
			User user=(User) session.getAttribute("userTopLogin");
			System.out.println(user.getUserId());
			//将获取的信息存入到实体类中
			AnswerBean ab=new AnswerBean(content, user.getUserId(), qId);
			//通过service进行增加操作
			boolean flag=as.getInsertAnswerBeanNow(ab);
			//将返回值flag传到页面中
			response.getWriter().print(flag);
			//页面的转发功能，转发到指定jsp页面
			request.getRequestDispatcher("single.jsp").forward(request, response);
		//增加评论功能
		}else if("insertComment".equals(op)) {
			//从页面中获取增加评论的信息
			String cContent=request.getParameter("cContent");
			//int userId=Integer.parseInt(request.getParameter("userId"));
			int aidIn=Integer.parseInt(request.getParameter("aid"));
			int cqId=Integer.parseInt(request.getParameter("qId"));
			User user=(User) session.getAttribute("userTopLogin");
			//将获取到 的信息存入到实体类中
			CommentBean cb=new CommentBean(cContent, user.getUserId(), aidIn, cqId);
			//通过service进行增加操作
			boolean flag=cbs.getInsertComment(cb);
			//将返回值flag传到页面
			response.getWriter().print(flag);
		//实现回答点赞功能
		}else if("updateAnswerPraise".equals(op)) {
			//从页面中获取信息
			int paPraise=Integer.parseInt(request.getParameter("aPraise"));
			int paid=Integer.parseInt(request.getParameter("aid"));
			//将获取的信息存入到实体类中
			AnswerBean ab=new AnswerBean(paPraise, paid);
			//通过service进行修改操作
			boolean flag=as.getUpdateAnswerBeanPraise(ab);
			//通过service进行通过paid查询操作
			List<AnswerBean> list=as.getAnswerBeanById(paid);
			//将集合中的点赞数传到页面
			response.getWriter().print(list.get(0).getaPraise());
		//实现评论点赞功能
		}else if("updateCommentPraise".equals(op)) {
			//从页面中获取信息
			int pcPraise=Integer.parseInt(request.getParameter("cPraise"));
			int pcid=Integer.parseInt(request.getParameter("cId"));
			//将获取的信息存入到实体类中
			CommentBean cb=new CommentBean(pcPraise, pcid);
			//通过service进行修改操作
			boolean flag=cbs.getUpdateComment(cb);
			//通过service进行通过paid查询操作
			List<CommentBean> list=cbs.getQueryCommentById(pcid);
			//将集合中的点赞数传到页面
			response.getWriter().print(list.get(0).getcPraise());
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
